/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dnbW.uiW.queryW.modelW;

import dnbW.dbW.PathExpression;
import dnbW.entityW.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author zhouyong
 */
public class XComparisonNodeObject extends ComparisonNodeObject {

    private JoinNodeObject joinBridge;

    public JoinNodeObject getJoinBridge() {
        return joinBridge;
    }

    public void setJoinBridge(JoinNodeObject joinBridge) {
        JoinNodeObject oldBridge = this.getJoinBridge();
        this.joinBridge = joinBridge;
//        super.propertyChangeSupport.firePropertyChange("joinBridge", oldBridge, getJoinBridge());
    }

    public XComparisonNodeObject(
            DefaultMutableTreeNode node,
            PathExpression pathExpression,
            String operator,
            String value) {
        super(node, pathExpression,operator, value);
    }

    @Override
    public Condition getCondition(PathExpression outputPathExp) {
        Path outputPath = Path.buildPathFromPathExp(outputPathExp.getDataSource(), outputPathExp.getFullPathText());
        Condition outputCondition = new Condition(outputPath);

        PathExpression pathExp1, pathExp2;
        if (this.joinBridge.getLeftPath().isSameDataSource(outputPathExp)) {
            pathExp1 = this.joinBridge.getLeftPath();
            pathExp2 = this.joinBridge.getRightPath();
        }
        else if (this.joinBridge.getRightPath().isSameDataSource(outputPathExp)) {
            pathExp1 = this.joinBridge.getRightPath();
            pathExp2 = this.joinBridge.getLeftPath();
        }
        else return null;

        Path path1 = Path.buildPathFromPathExp(pathExp1.getDataSource(), pathExp1.getFullPathText());
        Condition condition1 = new Condition(path1);
        outputCondition.getGroup().addCondition(condition1);

        Path path2 = Path.buildPathFromPathExp(pathExp2.getDataSource(), pathExp2.getFullPathText());
        Condition condition2 = new Condition(path2);
        condition1.getGroup().addCondition(condition2);

        Path thisPath = Path.buildPathFromPathExp(this.pathExpression.getDataSource(), this.pathExpression.getFullPathText());
        Operator operator = Operator.createOperator(this.operator);
        String value = this.value;
        Condition thisCondition = new Condition(thisPath);
        thisCondition.setValueCondition(operator, value);
        condition2.getGroup().addCondition(thisCondition);

        return outputCondition;
    }

//    @Override
    public Condition getCondition(PathExpression outputPathExp, ConditionCluster cluster) {
        Path outputPath = Path.buildPathFromPathExp(outputPathExp.getDataSource(), outputPathExp.getFullPathText());
        Condition outputCondition = new Condition(outputPath);

        PathExpression pathExp1, pathExp2;
        if (this.joinBridge.getLeftPath().isSameDataSource(outputPathExp)) {
            pathExp1 = this.joinBridge.getLeftPath();
            pathExp2 = this.joinBridge.getRightPath();
        }
        else if (this.joinBridge.getRightPath().isSameDataSource(outputPathExp)) {
            pathExp1 = this.joinBridge.getRightPath();
            pathExp2 = this.joinBridge.getLeftPath();
        }
        else return null;

        Path path1 = Path.buildPathFromPathExp(pathExp1.getDataSource(), pathExp1.getFullPathText());
        Condition condition1 = new Condition(path1);
        outputCondition.getGroup().addCondition(condition1);

        Path path2 = Path.buildPathFromPathExp(pathExp2.getDataSource(), pathExp2.getFullPathText());
        Condition condition2 = new Condition(path2);
        condition1.getGroup().addCondition(condition2);

        Path thisPath = Path.buildPathFromPathExp(this.pathExpression.getDataSource(), this.pathExpression.getFullPathText());
        Operator operator = Operator.createOperator(this.operator);
        String value = this.value;
        Condition thisCondition = new Condition(thisPath);
        thisCondition.setValueCondition(operator, value);
        condition2.getGroup().addCondition(thisCondition);
//        for (ConditionNodeLeafObject obj : cluster.getConditions()) {
//            if (obj instanceof ComparisonNodeObject) {
//                ComparisonNodeObject other = (ComparisonNodeObject)obj;
//                Path otherPath = Path.buildPathFromPathExp(other.getPathExpression().getDataSource(), other.getPathExpression().getFullPathText());
//                Operator otherOperator = Operator.createOperator(other.operator);
//                String otherValue = other.value;
//                Condition otherCondition = new Condition(otherPath);
//                otherCondition.setValueCondition(otherOperator, otherValue);
//                thisCondition.getGroup().addCondition(otherCondition);
//            }
//        }
        return outputCondition;
    }
}
